package com.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.mapper.CheckItemMapper;
import com.health.pojo.CheckItem;
import com.health.service.CheckGroupCheckItemService;
import com.health.service.CheckItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 检查项服务实现类
 */
@Service
@RequiredArgsConstructor
public class CheckItemServiceImpl
        extends ServiceImpl<CheckItemMapper, CheckItem>
        implements CheckItemService {

    private final CheckGroupCheckItemService checkGroupCheckItemService;

    /**
     * 删除检查项
     *
     * @param id
     */
    @Override
    public void deleteCheckItem(Integer id) {
        //1. 判断是否与检查组有关联
        //select count(*) from t_checkgroup_checkitem where checkitem_id = ?
        Long count = checkGroupCheckItemService.query()
                .eq("checkitem_id", id)
                .count();
        if (count > 0) {
            //说明有关联数据，禁止删除，抛异常
            throw new RuntimeException("该数据被检查组关联，不能删除");
            //抛异常之后的代码不执行，直接去全局异常处理器
        }

        this.removeById(id);
    }
}
